﻿
@{
    ViewBag.Title = "采购入库编辑";

}
<link rel="stylesheet" href="../../Customiz/layui/css/layui.css" media="all">
<form class="layui-form" action="" lay-filter="example">
    <div class="layui-form" lay-filter="layuiadmin-form-admin" id="layuiadmin-form-admin" style="padding: 20px 30px 0 0;">
        <div class="layui-form-item">
            <label class="layui-form-label">军械名称</label>
            <div class="layui-input-block">
                <select name="Report_ArmyID" id="Report_ArmyID">
                    <option value=""></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">入库仓库</label>
            <div class="layui-input-block">
                <select name="Report_WarehouseID" id="Report_WarehouseID">
                    <option value=""></option>
                </select>
            </div>
        </div>       
        <div class="layui-form-item">
            <label class="layui-form-label">军械批次</label>
            <div class="layui-input-inline">
                <input type="text" name="Report_Batch" id="Report_Batch" lay-verify="required" placeholder="请输入批次" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">军械数量</label>
            <div class="layui-input-inline">
                <input type="text" name="Report_Num" id="Report_Num" lay-verify="required|num" placeholder="请输入数量" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">采购员</label>
            <div class="layui-input-block">
                <select name="Report_UserID" id="Report_UserID">
                    <option value=""></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">配送人姓名</label>
            <div class="layui-input-inline">
                <input type="text" name="Report_TransportName" id="Report_TransportName" lay-verify="required" placeholder="请输入配送人姓名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">联系方式</label>
            <div class="layui-input-inline">
                <input type="text" name="Report_TransportPhone" id="Report_TransportPhone" lay-verify="required" placeholder="请输入联系方式" autocomplete="off" class="layui-input">
            </div>
        </div>
        
        <div>
            <button class="layui-btn layui-btn-primary layui-btn-sm" id="Ord_Quxiao" style="float:right">取消</button>
            <button class="layui-btn layui-btn-normal layui-btn-sm" lay-submit lay-filter="Ord-Report-Add-search" style="float:right;margin-right:20px">确定</button>
        </div>
    </div>
</form>
<script src="../../Customiz/scripts/jquery-3.4.1.min.js"></script>
<script src="../../Customiz/layui/layui.js"></script>  
<script>
    //根据批次获取是否军械批次重复
    function selectcount(value) {
        var count = 0;
        $.ajax({
            url: '/Army/GetReportBatchCount',
            dataType: 'json',
            type: 'Post',
            async: false,
            data: { Army_Batch: value },
            success: function (data) {
                count= data;
            }
        })
        return count;
    }




    layui.use(['table'], function () {
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;
        $("#Ord_Quxiao").click(function () {
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引

            parent.layer.close(index); //再执行关闭
        });

        //表单验证
        form.verify({
            num: function (value) {
                if (!/^[0-9]+$/.test(value)) {
                    return '请输入数字！';
                }

            }

        }); //用户(采购员id)
        $.ajax({
            url: '/User/GetUser',
            dataType: 'json',
            type: 'Post',
            async: false,
            data:{ name:"", dept:"", role:"1"},
            success: function (data) {
                $.each(data.data, function (index, item) {
                    $('#Report_UserID').append(new Option(item.User_Name, item.User_ID)); // 下拉菜单里添加元素
                });
                layui.form.render("select");
                //重新渲染 固定写法
            }
        })
        //军械名称
        $.ajax({
            url: '/Army/GetArmyData',
            dataType: 'json',
            type: 'Post',
            async: false,
            success: function (data) {
                $.each(data.data, function (index, item) {
                    $('#Report_ArmyID').append(new Option(item.Army_Name, item.Army_ID)); // 下拉菜单里添加元素
                });
                layui.form.render("select");
                //重新渲染 固定写法
            }
        })
        //军械仓库
        $.ajax({
            url: '/Index/GetWarehouseData',
            dataType: 'json',
            type: 'Post',
            async: false,
            success: function (data) {
                $.each(data.data, function (index, item) {
                    $('#Report_WarehouseID').append(new Option(item.Warehouse_Name, item.Warehouse_ID)); // 下拉菜单里添加元素
                });
                layui.form.render("select");
                //重新渲染 固定写法
            }
        })
        if(@ViewBag.Type==2)
        {
            var select = 'dd[lay-value=' +  @ViewBag.Report_ArmyID+ ']';
            $('#Report_ArmyID').siblings("div.layui-form-select").find('dl').find(select).click();

            var select = 'dd[lay-value=' +  @ViewBag.Report_WarehouseID+ ']';
            $('#Report_WarehouseID').siblings("div.layui-form-select").find('dl').find(select).click();

            var select = 'dd[lay-value=' + @ViewBag.Report_UserID+ ']';
            $('#Report_UserID').siblings("div.layui-form-select").find('dl').find(select).click();

            $("#Report_Batch").val('@ViewBag.Report_Batch');
            $("#Report_Num").val('@ViewBag.Report_Num');
            $("#Report_TransportName").val('@ViewBag.Report_TransportName');
            $("#Report_TransportPhone").val('@ViewBag.Report_TransportPhone');

        }
        //监听提交
        form.on('submit(Ord-Report-Add-search)', function (data) {
            var field = data.field;
            var Report_ArmyIDBool=true;
            var Report_WarehouseIDBool=true;
            var Report_UserIDBool=true;
            if($("#Report_ArmyID").val().length<=0)
            {
                Report_ArmyIDBool=false;
                layer.msg('请选择军械名称！', { icon: 7 });
            }
            if($("#Report_WarehouseID").val().length<=0)
            {
                Report_WarehouseIDBool=false;
                layer.msg('请选择仓库！', { icon: 7 });
            }
            if($("#Report_UserID").val().length<=0)
            {
                Report_UserIDBool=false;
                layer.msg('请选择采购员！', { icon: 7 });
            }
            if(Report_ArmyIDBool==true&&Report_WarehouseIDBool==true&&Report_WarehouseIDBool==true)
            {
                if(@ViewBag.Type==1)
                {
                    if (selectcount(field.Report_Batch) > 0)
                    {
                        layer.msg('军械批次存在重复！', { icon: 7 });
                    }
                    else
                    {
                        $.ajax({
                            cache: false,
                            type: "Post",
                            async: false,
                            url: '/Army/AddReport', //模拟接口
                            data: {
                                Report_ArmyID: field.Report_ArmyID,
                                Report_WarehouseID: field.Report_WarehouseID,
                                Report_Batch:field.Report_Batch,
                                Report_Num: field.Report_Num,
                                Report_UserID:field.Report_UserID ,
                                Report_TransportName:field.Report_TransportName ,
                                Report_TransportPhone:field.Report_TransportPhone
                            },
                            dataType: 'json',
                            success: function (data) {
                                if (data == true) {
                                    parent.layer.msg("新增成功！", { icon: 1 });

                                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                    parent.layer.close(index); //再执行关闭




                                } else {
                                    parent.layer.msg("新增失败！", { icon: 2 });
                                }
                            }
                        })
                    }
                }
                else
                {
                    //提交 Ajax 成功后，静态更新表格中的数据
                    $.ajax({
                        cache: false,
                        type: "Post",
                        async: false,
                        url: '/Army/UpdateReportData', //模拟接口
                        data: {
                            Report_ID:'@ViewBag.ID',
                            Report_ArmyID: field.Report_ArmyID,
                            Report_WarehouseID: field.Report_WarehouseID,
                            Report_Batch: field.Report_Batch,
                            Report_Num: field.Report_Num,
                            Report_UserID: field.Report_UserID,
                            Report_TransportName: field.Report_TransportName,
                            Report_TransportPhone: field.Report_TransportPhone
                        },                           
                        dataType: 'json',
                        success: function (data) {
                            if (data == true) {
                                parent.layer.msg("修改成功！", { icon: 1 });

                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭

                            } else {
                                parent.layer.msg("修改失败！", { icon: 2 });
                            }
                        }
                    });
                }
            }
        })

    })

    </script>
